﻿using System;
using System.Data.Services.Client;

namespace InstanceController.RolesPerfMonDAL
{
	public static class CounterSettingsBatchOperation
	{
		// Performing Entity Group Transactions
		#region CRUD Entity Group Transactions (Batch) http://msdn.microsoft.com/en-us/library/dd894038.aspx

		private const int MaxEntityCount = 101; // max is 100, testing for: < MaxEntityCount

		/// <summary>
		/// INSERT - Add and entity object to the specified context to create a batch of entities.
		/// These do not have to be the same type of entity.
		/// </summary>
		/// <param name="newItem"></param>
		/// <param name="ctx"></param>
		public static int AddBatchInsertEntityItem(Object newItem, CounterSettingsDataServiceContext ctx)
		{
			// Is this entity already in the list?
			if (ctx.Entities.Contains(ctx.GetEntityDescriptor(newItem)))
				return -1;

			var entityCount = ctx.Entities.Count;
			if (entityCount < MaxEntityCount)
			{
				ctx.AddObject(AzureTableInitialization.CounterSettingsTableName, newItem);
				entityCount++;
			}
			return entityCount;
		}

		/// <summary>
		/// UPDATE - Add and entity object to the specified context to create a batch of entities.
		/// These do not have to be the same type of entity.
		/// </summary>
		/// <param name="itemToUpdate"></param>
		/// <param name="ctx"></param>
		/// <returns></returns>
		public static int AddBatchUpdateEntityItem(Object itemToUpdate, CounterSettingsDataServiceContext ctx)
		{
			// Is this entity already in the list?
			if (ctx.Entities.Contains(ctx.GetEntityDescriptor(itemToUpdate)))
				return -1;

			var entityCount = ctx.Entities.Count;
			if (entityCount < MaxEntityCount)
			{
				ctx.UpdateObject(itemToUpdate);
				entityCount++;
			}
			return entityCount;
		}

		/// <summary>
		/// DELETE - Add and entity object to the specified context to create a batch of entities.
		/// These do not have to be the same type of entity.
		/// </summary>
		/// <param name="itemToDelete"></param>
		/// <param name="ctx"></param>
		/// <returns></returns>
		public static int AddBatchDeleteEntityItem(Object itemToDelete, CounterSettingsDataServiceContext ctx)
		{
			// Is this entity already in the list?
			if (ctx.Entities.Contains(ctx.GetEntityDescriptor(itemToDelete)))
				return -1;

			var entityCount = ctx.Entities.Count;
			if (entityCount < MaxEntityCount)
			{
				ctx.DeleteObject(itemToDelete);
				entityCount++;
			}
			return entityCount;
		}

		/// <summary>
		/// Save a batch of inserts using the specified context. Returns false if 
		/// insert fails.
		/// </summary>
		/// <param name="ctx"></param>
		/// <returns></returns>
		public static bool SaveBatch(CounterSettingsDataServiceContext ctx)
		{
			var retValue = true;
			if (ctx.Entities.Count == 0) return false;

			// All operations are executed as a single batch request.
			var response = ctx.SaveChanges(SaveChangesOptions.Batch);
			if (response.BatchStatusCode != 202)
			{
				//Todo: logging functions here...

				//string exception = "failed";
				retValue = false;
				/*
				 * The Table service supports a subset of the functionality defined by ADO.NET Data Services:
				 * 
				 * The Table service supports only a single change set within a batch. 
				 * The change set can include multiple insert, update, and delete operations. 
				 * If a batch includes more than one change set, the first change set will 
				 * be processed by the service, and additional change sets will be rejected 
				 * with status code 400 (Bad Request).
				 */
			}
			return retValue;
		}

		#endregion

	}
}